<template>
  <div class="login-container">
    <canvas class="background" />
    <el-row type="flex" class="container">
      <!--【一】 左侧 -->
      <el-col
        :span="22"
        style="padding-top: 50px;padding-left:20px;padding-bottom: 50px;"
        class="hidden-sm-and-down"
      >
        <el-row>
          <img src="../../assets/login/login1.svg" style="width: 100%">
          <img src="../../assets/login/login2.svg" style="width: 100%;padding-top:50px">
        </el-row>
      </el-col>
      <!-- 【二】右侧 -->
      <!-- ## 登录表单 -->
      <el-col v-if="islogin" key="1" :span="2" style="margin: 0 auto;width:100%">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="Rules"
          class="login-form"
          auto-complete="on"
          label-position="top"
        >
          <el-row type="flex" style="padding-top:40px;padding-bottom:40px">
            <el-col style="display:flex;justify-content:center">
              <h2 style="min-width: 160px;margin-left:240px">登录账号</h2>
            </el-col>
            <el-col style="display:flex;align-items:end;">
              <el-link type="primary" :underline="false" @click="islogin =!islogin">没有账号 去注册</el-link>
            </el-col>
          </el-row>
          <el-col class="middle">
            <el-form-item prop="username" class="formitem" label="用户名">
              <span class="svg-container svg">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="用户名"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-form-item>

            <el-form-item prop="password" class="formitem" label="密码">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                show-password
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>
            <el-form-item class="formitem" prop="checked">
              <el-checkbox v-model="loginForm.checked">我已确定用户协议与隐私协议</el-checkbox>
            </el-form-item>
            <el-button
              :loading="loading"
              class="btn_login"
              round
              type="primary"
              style="width:50%;margin-bottom:30px;"
              @click.native.prevent="handleLogin"
            >登录</el-button>
          </el-col>
        </el-form>
      </el-col>
      <!-- ## 注册表单 -->
      <el-col v-else key="2" :span="2" style="margin: 0 auto;width:100%">
        <el-form
          ref="regForm"
          :model="regForm"
          :rules="Rules"
          class="login-form"
          auto-complete="on"
          label-position="top"
        >
          <el-row type="flex" style="padding-top:40px;padding-bottom:40px">
            <el-col style="display:flex;justify-content:center">
              <h2 style="min-width: 160px;margin-left:240px">注册账号</h2>
            </el-col>
            <el-col style="display:flex;align-items:end;">
              <el-link type="primary" :underline="false" @click="islogin =!islogin">已有账号 去登录</el-link>
            </el-col>
          </el-row>
          <el-col class="middle">
            <el-form-item prop="username" class="formitem" label="用户名">
              <span class="svg-container svg">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                ref="username"
                v-model="regForm.username"
                placeholder="用户名"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-form-item>

            <el-form-item prop="password" class="formitem" label="密码">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                ref="password"
                v-model="regForm.password"
                :type="passwordType"
                placeholder="密码"
                name="password"
                auto-complete="on"
                show-password
              />
            </el-form-item>

            <!-- <el-form-item prop="repassword" class="formitem" label="确认密码">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                ref="repassword"
                v-model="regForm.repassword"
                :type="passwordType"
                placeholder="密码"
                name="repassword"
                auto-complete="on"
                show-password
              />
            </el-form-item> -->

            <el-form-item class="formitem" prop="checked">
              <el-checkbox v-model="regForm.checked">我已确定用户协议与隐私协议</el-checkbox>
            </el-form-item>
            <el-button
              :loading="loading"
              round
              class="btn_reg"
              type="primary"
              style="width:50%;margin-bottom:30px;"
              @click.native.prevent="regBtn"
            >注册</el-button>
          </el-col>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { register } from '@/api/user.js'
import Particles from "particlesjs"

export default {
  name: 'Login',
  data() {
    return {
      // 显示登录还是注册
      islogin: true,
      // 登录表单数据
      loginForm: {
        username: '',
        password: '',
        checked: false

      },
      // 注册表单数据
      regForm: {
        username: '',
        password: '',
        repassword: '',
        checked: false
      },
      // 登录注册表单的校验
      Rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '用户名长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 16, message: '密码长度在 5 到 16 个字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { min: 5, max: 16, message: '密码长度在 5 到 16 个字符', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (value === '') {
              callback(new Error('请再次输入密码'))
            } else if (value !== this.regForm.password) {
              callback(new Error('两次输入密码不一致'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        checked: [
          { validator: (rule, value, callback) => {
            if (!value) {
              callback(new Error('请确认用户协议与隐私协议'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ]

      },
      loading: false,
      passwordType: 'password'
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      window.onload = function () {
        Particles.init({
          // 选择canvas画布元素
          selector: '.background',
          // 设置最大粒子数
          maxParticles: 100,
          // 设置粒子的移动速度
          speed: 1,
          // 设置粒子颜色
          color: '#8ACAFF',
          shape: 'triangle',
          onclick: true,
          // 开启点连线
          connectParticles: true,
          // 设置
          minDistance: 140,
          // 设置响应式配置项的调整
          responsive: [
            {
              // 当浏览器窗口宽小于768像素大小采用以下配置
              breakpoint: 768,
              options: {
                maxParticles: 300,
                color: '#8ACAFF',
                connectParticles: false
              }
            }, {
            // 当浏览器窗口宽小于425像素大小采用以下配置
              breakpoint: 425,
              options: {
                maxParticles: 100,
                connectParticles: false
              }
            }, {
              // 当浏览器窗口宽小于320像素大小采用以下配置
              breakpoint: 320,
              options: {
                maxParticles: 0
              }
            }
          ]
        })
      }
    },
    /** *
     * 登录按钮
     */
    handleLogin() {
      this.$refs.loginForm.validate(async isOK => {
        if (isOK) {
          const loginData = {
            username: this.loginForm.username,
            password: this.loginForm.password
          }
          this.loading = true
          await this.$store.dispatch('user/login', loginData) // 存储到vuex
          this.$router.push('/')
          this.loading = false
        }
      })
    },
    /** *
     * 注册按钮
     */
    regBtn() {
      this.$refs.regForm.validate(async isOK => {
        if (isOK) {
          this.loading = true
          const loginData = {
            username: this.regForm.username,
            password: this.regForm.password
          }
          await register(loginData)
          this.$message({
            message: '注册成功 登录！',
            type: 'success'
          })
          this.loading = false
          this.islogin = true
          this.loginForm.username = ''
          this.loginForm.password = ''
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #454545;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  .el-input {
    // display: inline-block;
    height: 47px;
    width: 65%;
    margin-left: 15px;

    input {
      background: transparent;
      padding: 12px 5px 12px 15px;
      color: #000000;
      height: 47px;
      caret-color: $cursor;
      border-radius: 20px;
      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px $bg inset !important;
      //   -webkit-text-fill-color: $cursor !important;
      // }
    }
  }
  .middle {
    display: flex;
    flex-direction: column;
    align-items: center;
    .formitem {
      width: 80%;
      min-width: 200px;
      border-radius: 10px;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.background{
  position: absolute;
  top: 0;
  left: 0;
  z-index: 0;
}
@media (min-width:463px) {
  .btn_login{
    position: relative;
    left: -63px;
  }
  .btn_reg{
    position: relative;
    left: -63px;
  }
}
h2 {
  font-size: 40px;
  font-weight: 200;
}
.login-container {
  position :absolute;
  z-index: 999;
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-self: center;
  // background-image: url(../../assets/loginbg.svg);
  background: linear-gradient(to right, #e7f0fd, #8ec5fc, #c2e9fb);
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  background-position: center center;
  .container {
    width: 60%;
    margin: auto auto;
    height: 70%;
    min-height: 70%;
    box-shadow: 9px 9px 10px rgba(0, 0, 0, 0.25);
    border-radius: 20px;
    background-color: #ffffff;
    align-items: center;
  }
  @media (max-width: 463px) {
    .container {
        width: 90%;
    }

}
  .login-form {
    position: relative;
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

}
</style>
